home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Clean 1.2.4 / IOInterface / dialogDef.icl < prev    next >
Encoding:
Modula Implementation  |  1997-04-23  |  9.6 KB  |  272 lines  |  [TEXT/3PRM]

  1. implementation module dialogDef;
  2.  
  3. //
  4. //    Dialog definitions
  5. //
  6.  
  7. import StdClass;
  8. import    StdInt, StdMisc, StdString, StdBool;
  9. import    mac_types, quickdraw, pointer;
  10. import    deltaFont, dialogLayout;
  11.  
  12.  
  13. ::    PosOrSize    =    Default
  14.                 |    Pixels Int Int;
  15.  
  16.  
  17. NoDefaultButton :== -3;
  18.  
  19. ModalHor        :== 8;
  20. MlessHor        :== 1;
  21. ModalVer        :== 28;
  22. MlessVer        :== 39;
  23. HorMargin        :== 12;
  24. VerMargin        :== 12;
  25. HorSpace        :== 16;
  26. VerSpace        :== 16;
  27. RB                :== HorMargin;
  28. R2                :== HorSpace;
  29. R1                :== VerSpace;
  30. N2                :== 8;
  31. N1                :== 8;
  32.  
  33.  
  34. //    Calculate the exact layout in pixels of a DialogDef (result: left,top,width,height,ddef').
  35.  
  36. CalculateDialogLayout :: !DialogMode !(DialogDef s io) -> (!Int,!Int,!Int,!Int,!DialogDef s io);
  37. CalculateDialogLayout mode dDef
  38.     =    (l,t, r-l,b-t, DialogHandleToDialogDef dH);
  39.     where {
  40.         (l,t, r,b)    = GetDialogRect dH;
  41.         dH            = DialogDefToDialogHandle mode dDef;
  42.     };
  43.  
  44. GetDialogRect :: !(DialogHandle s io) -> Rect;
  45. GetDialogRect (DialogH _ _ _ rect _ _ _) = rect;
  46.  
  47. DialogHandleToDialogDef :: !(DialogHandle s io) -> DialogDef s io;
  48. DialogHandleToDialogDef (DialogH id title _ _ _ items (DialogRest Command attrs defid))
  49.     =    CommandDialog id title attrs defid items;
  50. DialogHandleToDialogDef (DialogH id title _ _ _ [set  =:DialogButton _ _ _ _ setf,
  51.                                                  reset=:DialogButton _ _ _ _ resetf : items]
  52.                                                  rest =:(DialogRest Property attrs _))
  53.     =    PropertyDialog id title attrs setf resetf items;
  54.  
  55.  
  56. //    Transform NoticeDefs and DialogDefs into the internally used structure.
  57.  
  58. NoticeDefToNoticeHandle :: !NoticeDef -> NoticeHandle s io;
  59. NoticeDefToNoticeHandle (Notice lines defbut buttons)
  60. |    string == ""    = NoticeH rect buts;
  61.                     = NoticeH rect [Head buts, text : Tail buts];
  62.     where {
  63.         rect              = CenterDialogRect ModalVer (0,0,wid,hgt);
  64.         buts              = ShiftNoticeButtons (bwid - twid) buts`;
  65.         (bwid,buts`)      = NoticeButsToDialogButs (N2 + twid) btop [defbut:buttons];
  66.         text              = StaticText (-1) (ItemBox N2 N1 (twid + 4) thgt) string;
  67.         (twid,thgt,string)= NoticeLinesToText lines;
  68.         wid               = N2 + ( Max twid bwid  + N2);
  69.         hgt               = btop + (ButHgt + N1);
  70.         btop              = N1 + (thgt + N2);
  71.     };
  72.  
  73. /*    NB: The functions ChangeSetFunction and ChangeResetFunction (deltaDialog) depend on the
  74.         fact that the id's of the Set and Reset button are -1 resp. -2 !!! */
  75.  
  76. DialogDefToDialogHandle    :: !DialogMode !(DialogDef s io) -> DialogHandle s io;
  77. DialogDefToDialogHandle mode (PropertyDialog id title attrs set reset items)
  78.     =    DialogH id title Modeless rect [] [setbut,resbut:items`] rest;
  79.     where {
  80.         rect        = ExtendPropertyRect pos (CalcDialogRect MlessHor MlessVer w h pos size items`);
  81.         setbut      = DialogButton (-1) setbox "Apply"  Able set;
  82.         resbut      = DialogButton (-2) resbox "Cancel" Able reset;
  83.         (w,h,items`)= CalcItemBoxes margins spaces items;
  84.         rest        = DialogRest Property attrs (-1);
  85.         pos            = AttrPosition     attrs;
  86.         size        = AttrSize         attrs;
  87.         margins        = AttrMargins     attrs;
  88.         spaces        = AttrItemSpaces attrs;
  89.         setbox      = ItemBox  l                 t MinButWid ButHgt;
  90.         resbox      = ItemBox (l-(MinButWid+hs)) t MinButWid ButHgt;
  91.         wid            = rr - rl;
  92.         hgt            = rb - rt;        (rl,rt,rr,rb) = rect;
  93.         (hm,vm)        = margins;
  94.         (hs,vs)        = spaces;
  95.         l            = wid - (MinButWid + hm);
  96.         t            = hgt - (ButHgt    + vm);
  97.     };
  98. DialogDefToDialogHandle mode (CommandDialog id title attrs defid items)
  99.     =    DialogH id title mode rect [] items` rest;
  100.     where {
  101.         rect        = CalcDialogRect hofs vofs w h pos size items`;
  102.         (w,h,items`)= CalcItemBoxes margins spaces items;
  103.         rest        = DialogRest Command attrs (CheckDefaultItemId defid items items);
  104.         (hofs,vofs)    = DialogModeToOffset mode;
  105.         pos            = AttrPosition     attrs;
  106.         size        = AttrSize         attrs;
  107.         margins        = AttrMargins     attrs;
  108.         spaces        = AttrItemSpaces attrs;
  109.     };
  110.  
  111.  
  112. //    Retrieve position, size, margins and items spaces from the attribute list.
  113.  
  114. AttrPosition    :: ![DialogAttribute] -> PosOrSize;
  115. AttrPosition    [DialogPos hor ver        : _]    = Pixels (HorMeasureToPixels hor) (VerMeasureToPixels ver);
  116. AttrPosition    [_                        : rest]    = AttrPosition rest;
  117. AttrPosition    _                                = Default;
  118.  
  119. AttrSize        :: ![DialogAttribute] -> PosOrSize;
  120. AttrSize        [DialogSize wid hgt     : _]    = Pixels (HorMeasureToPixels wid) (VerMeasureToPixels hgt);
  121. AttrSize        [_                        : rest]    = AttrSize rest;
  122. AttrSize        _                                = Default;
  123.  
  124. AttrMargins        :: ![DialogAttribute] -> (!Int,!Int);
  125. AttrMargins        [DialogMargin wid hgt    : rest]    = (HorMeasureToPixels wid, VerMeasureToPixels hgt);
  126. AttrMargins        [_                        : rest] = AttrMargins rest;
  127. AttrMargins        _                                = (HorMargin, VerMargin);
  128.  
  129. AttrItemSpaces    :: ![DialogAttribute] -> (!Int,!Int);
  130. AttrItemSpaces    [ItemSpace wid hgt        : _]    = (HorMeasureToPixels wid, VerMeasureToPixels hgt);
  131. AttrItemSpaces    [_                        : rest]    = AttrItemSpaces rest;
  132. AttrItemSpaces    _                                = (HorSpace, VerSpace);
  133.  
  134.  
  135. //    CheckDefaultItemId checks whether the default item is indeed a button.
  136.  
  137. CheckDefaultItemId    :: !DialogItemId ![DialogItem s io] ![DialogItem s io] -> DialogItemId;
  138. CheckDefaultItemId defid [DialogButton id ps tt ab bf : rest] items
  139.     | defid == id =  defid;
  140.     =  CheckDefaultItemId defid rest items;
  141. CheckDefaultItemId defid [DialogIconButton id ps pd il ab bf : rest] items
  142.     | defid == id =  defid;
  143.     =  CheckDefaultItemId defid rest items;
  144. CheckDefaultItemId defid [item : rest] items =  CheckDefaultItemId defid rest items;
  145. CheckDefaultItemId defid [] items =  GetIdOfFirstButton items;
  146.  
  147. GetIdOfFirstButton    :: ![DialogItem s io] -> DialogItemId;
  148. GetIdOfFirstButton [DialogButton     id ps tt ab bf    : rest] =  id;
  149. GetIdOfFirstButton [DialogIconButton id ps pd il ab bf : rest] =  id;
  150. GetIdOfFirstButton [item : rest] =  GetIdOfFirstButton rest;
  151. GetIdOfFirstButton [] =  NoDefaultButton;
  152.  
  153.  
  154. //    Calculate (left,top,right,bottom) of a dialog.
  155.  
  156. CalcDialogRect :: !Int !Int !Int !Int !PosOrSize !PosOrSize ![DialogItem s io] -> Rect;
  157. CalcDialogRect hofs vofs w h Default size items
  158.     =    CenterDialogRect vofs (0,0, wid,hgt);
  159.     where {
  160.         (wid, hgt) = CalcDialogSize w h size;
  161.     };
  162. CalcDialogRect hofs vofs w h (Pixels hpos vpos) size items
  163. |    rl >= sl && rt >= st && rr <= sr && rb <= sb    = (rl,rt,rr,rb);
  164.                                                     = CenterDialogRect vofs (rl,rt,rr,rb);
  165.     where {
  166.         (sl`,st`, sr,sb, _)    = QScreenRect NewToolbox;
  167.         sl                    = sl` + hofs;
  168.         st                    = st` + vofs;
  169.         rl                    = sl  + hpos;
  170.         rt                    = st  + vpos;
  171.         rr                    = rl  + wid;
  172.         rb                    = rt  + hgt;
  173.         (wid, hgt)            = CalcDialogSize w h size;
  174.     };
  175.  
  176. CalcDialogSize :: !Int !Int !PosOrSize -> (!Int,!Int);
  177. CalcDialogSize w h Default        = (w, h);
  178. CalcDialogSize _ _ (Pixels w h)    = (w, h);
  179.  
  180.  
  181. //    Center a dialog on the screen.
  182.  
  183. CenterDialogRect :: !Int !Rect -> Rect;
  184. CenterDialogRect offs (rl,rt,rr,rb)
  185. |    leftvis && topvis    = (l,t,        r,        b);
  186. |    leftvis                = (l,st,    r,        st+b-t);
  187. |    topvis                = (sl,t,    sl+r-l,    b);
  188.                         = (sl,st,    sl+r-l,    st+b-t);
  189.     where {
  190.         l        = midh - rwid / 2;            r                = l + rwid;
  191.         t        = st + (shgt - rhgt) / 3;    b                = t + rhgt;
  192.         rwid    = rr - rl;                    rhgt            = rb - rt;
  193.         midh    = sl + (sr - sl) / 2;        shgt            = sb - st;
  194.         leftvis    = l >= sl;                    topvis            = t >= st;
  195.         st        = st` + offs;                (sl,st`,sr,sb,_)= QScreenRect NewToolbox;
  196.     };
  197.  
  198. /*    A PropertyDialog has two predefined buttons for which room must be
  199.     reserved in the dialog's rectangle.
  200. */
  201.  
  202. ExtendPropertyRect :: !PosOrSize !Rect -> Rect;
  203. ExtendPropertyRect Default (rl,rt, rr,rb)
  204. |    rr - rl  >= bwid    = CenterDialogRect MlessVer (rl,rt, rr,        rb+R1+ButHgt);
  205.                         = CenterDialogRect MlessVer (rl,rt, rl+bwid,rb+R1+ButHgt);
  206.     where {
  207.         bwid    = R2 + but + but;
  208.         but        = MinButWid + R2;
  209.     };
  210. ExtendPropertyRect position (rl,rt, rr,rb)
  211. |    rr - rl  >= bwid    = (rl,rt, rr,      rb+R1+ButHgt);
  212.                         = (rl,rt, rl+bwid,rb+R1+ButHgt);
  213.     where {
  214.         bwid    = R2 + but + but;
  215.         but        = MinButWid + R2;
  216.     };
  217.  
  218. /*    A modal dialog has no title bar. Therefore its vertical screen offset is
  219.     slightly smaller.
  220. */
  221.  
  222. DialogModeToOffset    :: !DialogMode -> (!Int,!Int);
  223. DialogModeToOffset Modal    = (ModalHor,ModalVer);
  224. DialogModeToOffset Modeless    = (MlessHor,MlessVer);
  225.  
  226.  
  227. //    Functions to transform Notice attributes into dialog items.
  228.  
  229. NoticeLinesToText    :: ![String] -> (!Int,!Int,!String);
  230. NoticeLinesToText lines =  MakeNoticeText 0 0 "" DialogFont lines;
  231.  
  232. MakeNoticeText    :: !Int !Int !String !Font ![String] -> (!Int,!Int,!String);
  233. MakeNoticeText wid hgt text dfont [line]
  234.     =     (Max wid (FontStringWidth line dfont),  hgt + 4  + LineHeight, text +++ line);
  235. MakeNoticeText wid hgt text dfont [line:lines]
  236.     =     MakeNoticeText (Max wid linewid) (hgt + LineHeight) (text +++  line +++ "\n" ) dfont lines;
  237.         where {
  238.         linewid= FontStringWidth line dfont;
  239.         };
  240. MakeNoticeText wid hgt text dfont [] =  (wid,hgt,text);
  241.  
  242. NoticeButsToDialogButs    :: !Int !Int ![NoticeButtonDef] -> (!Int,![DialogItem s io]);
  243. NoticeButsToDialogButs rgt top nbuts =  MakeNoticeButtons 0 [] DialogFont rgt top nbuts;
  244.  
  245. MakeNoticeButtons    :: !Int ![DialogItem s io] !Font !Int !Int ![NoticeButtonDef]
  246.     ->    (!Int,![DialogItem s io]);
  247. MakeNoticeButtons wid dbuts dfont rgt top [NoticeButton id title : nbuts]
  248.     =     MakeNoticeButtons (wid + bwid`) [dbut:dbuts] dfont (rgt - bwid`) top nbuts;
  249.         where {
  250.         dbut = DialogButton id ibox title Able NButFunc;
  251.         ibox = ItemBox (rgt - bwid) top bwid ButHgt;
  252.         bwid = Max MinButWid (R1 +  FontStringWidth title dfont );
  253.         bwid`= bwid + R2;
  254.         };
  255. MakeNoticeButtons wid dbuts dfont rgt top [] =  (wid - R2,dbuts);
  256.  
  257. // NButFunc    :: DialogInfo * s * io -> (*s, *io);
  258. NButFunc dlog s io =  (s,io);
  259.  
  260. ShiftNoticeButtons    :: !Int ![DialogItem s io] -> [DialogItem s io];
  261. ShiftNoticeButtons offs items
  262.     | offs <= 0 =     Reverse items [];
  263.     =     ShiftButtons [] offs items;
  264.  
  265. ShiftButtons    :: ![DialogItem s io] !Int ![DialogItem s io] -> [DialogItem s io];
  266. ShiftButtons sbuts offs [DialogButton i (ItemBox lft top wid hgt) t s f : buts]
  267.     =     ShiftButtons [sbut:sbuts] offs buts;
  268.         where {
  269.         sbut= DialogButton i (ItemBox (lft + offs) top wid hgt) t s f;
  270.         };
  271. ShiftButtons sbuts offs [] =  sbuts;
  272.